package com.google.code.xconf.schema.definition.value.options;

import com.google.code.xconf.utils.NumberTools;
import com.google.code.xconf.utils.StringTools;

public class NumericRange implements Options {

    private double minValue;
    private double maxValue;

    public NumericRange(String rangeExpr) {
        if (!isRangeExpr(rangeExpr)) {
            throw new RuntimeException("TDO EXEPt Thwin");
        }
        String[] parts = rangeExpr.split("-");
        double lefthand = NumberTools.doubleValue(parts[0]);
        double righthand = NumberTools.doubleValue(parts[1]);
        minValue = Math.min(lefthand, righthand);
        maxValue = Math.max(lefthand, righthand);
    }

    @Override
    public boolean matches(String value, boolean caseSensitive) {
        double d = NumberTools.doubleValue(value);
        return d >= minValue && d <= maxValue;
    }

    public static boolean isRangeExpr(String expr) {
        String[] parts = expr.split("-");
        return parts.length == 2 && StringTools.isNumberAssignable(parts[0]) && StringTools.isNumberAssignable(parts[1]);
    }

    @Override
    public String toString() {
        return minValue + "-" + maxValue;
    }
}
